/**
 *博尔信基础开发平台
 *<br> 版权声明：厦门博尔信软件有限公司版权所有,违者必究
 *<br> Copyright:  Copyright (c) 2012
 *<br> Company:pearlsoft
 *<br> @author 蒋勇华
 *<br> 2012-5-19
 *<br> @version 1.0
 *————————————————————————————————————
 *修改记录
 *    修改者：
 *    修改时间：
 *    修改原因：
 *——————————————————————————————————————
 */
package com.pearlsoft.webapp.sysmgr.code;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

import com.pearlsoft.webapp.core.annotation.QueryHandlerComponent;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Query;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.dataset.SimpleRecord;
import com.pearlsoft.webapp.core.dataset.spi.ContextQueryHandler;
import com.pearlsoft.webapp.sysmgr.code.service.CodeService;

/**
 *描述：
 * 
 * 
 */
@QueryHandlerComponent(name = "CodeQueryHandler")
public class CodeQueryHandler extends ContextQueryHandler {

    public void handler(Query query, DataSet ds) throws Exception {

    }

    /***
     * 得到下拉框的字典表
     * @param query
     * @param dataSet
     * @throws Exception
     */
    public void getComboxCode(Query query, DataSet dataSet) throws Exception {
        String codeType = (String) query.get("codeType");
        CodeService codeService = this.getBean("codeService", CodeService.class);
        List<Record> list = codeService.getCodeList(codeType);
        dataSet.setRecords(list);
    }

    public void getCodeCatalog(Query query, DataSet dataSet) throws Exception {
        JdbcTemplate jdbcTemplate = this.getJdbcTemplate();
        String codeType = (String) query.get("codeType");
        List<Map<String, Object>> result = jdbcTemplate.queryForList(
                "SELECT CODE_TYPE,CODE,CODE_TEXT FROM SYS_CODE WHERE CODE_TYPE=? ORDER BY CODE_ORDER", new Object[] { codeType });
        dataSet.newRecord();
        Record record = dataSet.getCurrentRecord();
        List<Map<String, Object>> records = new ArrayList<Map<String, Object>>();
        record.setValue("id", "root");
        record.setValue("text", "码表类型");
        record.setValue("leaf", "false");
        record.setValue("expanded", "true");
        record.setValue("children", records);
        for (Map<String, Object> r : result) {
            Record subRecord = new SimpleRecord();
            subRecord.setValue("id", r.get("CODE"));
            subRecord.setValue("text", r.get("CODE_TEXT"));
            subRecord.setValue("leaf", "true");
            records.add(subRecord.toMap());
        }
    }

    public void getCodeList(Query query, DataSet dataSet) throws Exception {
        JdbcTemplate jdbcTemplate = this.getJdbcTemplate();
        String codeType = (String) query.get("codeType");
        List<Map<String, Object>> result = jdbcTemplate.queryForList(
                "SELECT ID,CODE_TYPE,CODE,CODE_TEXT,CODE_ORDER FROM SYS_CODE WHERE CODE_TYPE=? ORDER BY CODE_ORDER",
                new Object[] { codeType });
        for (Map<String, Object> r : result) {
            dataSet.newRecord();
            for (Map.Entry<String, Object> entry : r.entrySet()) {
                dataSet.setValue(entry.getKey(), entry.getValue());
            }
        }
    }

    public void getCode(Query query, DataSet dataSet) throws Exception {
        JdbcTemplate jdbcTemplate = this.getJdbcTemplate();
        String id = (String) query.get("objId");
        List<Map<String, Object>> result = jdbcTemplate.queryForList(
                "SELECT CODE_TYPE,CODE,CODE_TEXT,CODE_ORDER FROM SYS_CODE WHERE ID=?", new Object[] { id });
        dataSet.newRecord();
        if (!result.isEmpty()) {
            for (Map.Entry<String, Object> entry : result.get(0).entrySet()) {
                dataSet.setValue(entry.getKey(), entry.getValue());
            }
        }
    }
}
